package com.zwps.biz.domain.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.model.dto.zhakou.ZhaKouDTO;
import com.zwps.biz.dal.db.dao.ZZhakongDAO;
import com.zwps.biz.dal.db.object.ZZhakongDO;
import com.zwps.biz.domain.service.ZZhakongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
public class ZZhakongServiceImpl extends ServiceImpl<ZZhakongDAO, ZZhakongDO>
    implements ZZhakongService{
    @Autowired
    private ZZhakongDAO zZhakongDAO;

    @Override
    public IPage<Map<String, Object>> getGateData(ZhaKouDTO zhaKouDTO) {
        if (Objects.isNull(zhaKouDTO)){
            zhaKouDTO=new ZhaKouDTO();
        }
        if (Objects.isNull(zhaKouDTO.getPageNum()) || zhaKouDTO.getPageNum() <= 0) {
            zhaKouDTO.setPageNum(1);
        }
        if (Objects.isNull(zhaKouDTO.getPageSize()) || zhaKouDTO.getPageSize() <= 0) {
            zhaKouDTO.setPageSize(10);
        }
        String pinyin=null;
        if (null!=zhaKouDTO.getName() && zhaKouDTO.getName()!="") {
            pinyin= PinyinUtil.getPinyin(zhaKouDTO.getName().substring(0, Math.min(zhaKouDTO.getName().length(), 2)));
            pinyin = pinyin.replaceAll("\\s+", "");
        }
        List<String> columnNames = zZhakongDAO.getCloumName(pinyin);
        List<String> allColumns = new ArrayList<>(columnNames);
        allColumns.add("id");
        allColumns.add("data_time");

        QueryWrapper<ZZhakongDO> wrapper = new QueryWrapper<>();
        wrapper.select(allColumns.toArray(new String[0]));
        wrapper.orderByDesc("data_time");

        Page<Map<String, Object>> page = new Page<>(zhaKouDTO.getPageNum(), zhaKouDTO.getPageSize());
        IPage<Map<String, Object>> paginatedResult = zZhakongDAO.selectMapsPage(page, wrapper);

        return paginatedResult;
    }

}




